package org.voovan.test.http;

import org.voovan.http.server.HttpRequest;
import org.voovan.http.server.HttpResponse;
import org.voovan.http.server.HttpRouter;
import org.voovan.http.server.WebServer;
import org.voovan.tools.TFile;
import org.voovan.tools.log.Logger;

import com.onemore.compare.CompareWms2Erp;
import com.onemore.compare.CompareB2cErpWms;
import com.onemore.compare.CompareErp2Wms;
import com.onemore.compare.CompareErpThd;
import com.onemore.task.OneServe;
import com.onemore.task.Onemore;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class WebServerDemo {
	private static byte[] fileContent = TFile.loadFileFromContextPath("WEBAPP/index.htm");
	
	public static void main(String[] args) {
		OneServe onemore = new OneServe();
		onemore.serve();
		WebServer webServer = WebServer.newInstance();
		webServer.get("/ab", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				resp.write(df.format(new Date()));
				resp.write("<br/>");
				resp.write(df.format(new Date()));
			}
		});

		//性能测试请求
        webServer.post("/test", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				resp.write(""+new Date().getTime());
				resp.write("<br/>");
				for(int i=0;i<10000;i++) {
					resp.write(""+i);
				}
				resp.write(""+new Date().getTime());

				System.out.println("post test");
				
			}
		});
		//性能测试请求
        webServer.get("/test", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				resp.write(""+new Date().getTime());
				resp.write("<br/>");
				for(int j=0;j<100;j++) {
					for(int i=0;i<100;i++) {
						resp.write(""+i);
					}
					resp.write("<br/>");
				}
				resp.write("<br/>");
				resp.write(""+new Date().getTime());
				System.out.println("get test");
			}
		});
        webServer.get("/data/erp2wms", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {
				CompareErp2Wms comSpjhd = new CompareErp2Wms();
				comSpjhd.process(req,resp);
			}
		});
        webServer.get("/data/erpthd", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {
				CompareErpThd comErpThd = new CompareErpThd();
				comErpThd.process(req,resp);
			}
		});
        webServer.get("/data/wms2erp", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {
				CompareWms2Erp comErp = new CompareWms2Erp();
				comErp.process(req,resp);
			}
		});
        webServer.get("/data/b2c", new HttpRouter() {
			public void process(HttpRequest req, HttpResponse resp) throws Exception {
				CompareB2cErpWms comB2c = new CompareB2cErpWms();
				comB2c.process(req,resp);
			}
		});
        
  		webServer.get("/bs", new HttpRouter(){
  			public void process(HttpRequest req, HttpResponse resp) throws Exception {
    			Onemore onemore = new Onemore();
  				onemore.disposeSubmit(req, resp);
  			}
  		});
  		webServer.post("/bs", new HttpRouter(){
  			public void process(HttpRequest req, HttpResponse resp) throws Exception {
  				Onemore onemore = new Onemore();
  				onemore.disposeSubmit(req, resp);
  				//webServer.restart();
  			}
  		});
  		webServer.get("/spd_check",new HttpRouter() {
			@Override
			public void process(HttpRequest request, HttpResponse response)
					throws Exception {
				// TODO Auto-generated method stub
				OneServe serv = new OneServe();
				serv.process_check_spd();
				
			}
  			
  		});
  		Runtime.getRuntime().addShutdownHook(new Thread() {
  		    public void run() {
  		        try {
  		            String restartCmd = "cmd /c start run.bat";//nohup java -jar xxx.jar
  		            run_cmd(restartCmd);
  		            Thread.sleep(2000);//等10秒，保证分身启动完成后，再关掉自己
  		            Logger.info("程序重启完成！");
  		        } catch (Exception e) {
  		        	Logger.error("重启失败，原因：", e);
  		        }
  		    }
  		});
        webServer.serve();
        
	}
	public static void run_cmd(String strcmd) {
		//
        Runtime rt = Runtime.getRuntime(); //Runtime.getRuntime()返回当前应用程序的Runtime对象
        Process ps = null;  //Process可以控制该子进程的执行或获取该子进程的信息。
        try {
            ps = rt.exec(strcmd);   //该对象的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序，并返回与该子进程对应的Process对象实例。
            ps.waitFor();  //等待子进程完成再往下执行。
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int i = ps.exitValue();  //接收执行完毕的返回值
        if (i == 0) {
            System.out.println("执行完成.");
        } else {
            System.out.println("执行失败.");
        }

        ps.destroy();  //销毁子进程
        ps = null;   
    }
}
